<?php 

if (! defined('BASEPATH')) exit('No direct script access');

class booking_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_by_id($booking_id = FALSE)
    {
        if ($booking_id === FALSE)
        {
            return FALSE;
        }
        
        $query = $this->db->where('id', $booking_id)
                          ->get('booking');
        if ($query->num_rows() == 1)
        {
            return $query->row();
        }
        else
        {
            return FALSE;
        }
    }
    
    
    function get_all($num = FALSE, $offset = FALSE) 
    {
        $query = $this->db->select('*' )
                          ->get('booking', $num, $offset);
        return $query->result();
    }
    
    function get_by_user_id($user_id,$num = FALSE, $offset = FALSE) 
    {
        $query = $this->db->select('booking.* ,schedule.* , movie.title as movie_title , branch.name as branch_name , 
                                    (select count(*) FROM  booking where schedule_id = schedule.id AND user_id = booking.user_id) as qty ' )
                          ->join('schedule', 'schedule.id = booking.schedule_id')
                          ->join('movie', 'movie.id = schedule.movie_id')
                          ->join('cinema', 'cinema.id = schedule.cinema_id')
                          ->join('branch', 'branch.id = cinema.branch_id')
                          ->where('booking.user_id', $user_id)
                          ->group_by(array("booking.schedule_id"))
                          ->get('booking', $num, $offset);
        return $query->result();
    }
    
    function create($schedule_id = FALSE ,$user_id = FALSE)
    {
        if ($schedule_id === FALSE OR $user_id === FALSE )
        {
            return FALSE;
        }
        
        $model = array(
                      'schedule_id'           => $schedule_id,    
                      'user_id'       => $user_id,
                     );
                     
        $this->db->insert('booking', $model);
        $model_id =$this->db->insert_id();       
        return  $model_id;
        
    }
    
    
    function update($branch_id = FAlSE , $name = FALSE, $description = FALSE, $address = FALSE)
    {
        if ($branch_id === FALSE OR $name === FALSE OR $description === FALSE OR $address === FALSE )
        {
            return FALSE;
        }
        $branch = array('name' => $name,
                         'description' => $description,
                         'address' => $address
                        );
        $this->db->where('id', $branch_id);
        $this->db->update('branch', $branch);
        return TRUE;
    }
    
    function delete($branch_id = FALSE)
    {
        if ($branch_id === FALSE)
        {
            return FALSE;
        }
        $this->db->where('id', $branch_id);
        $this->db->delete('branch');
        return TRUE;
    }
    
  
}
/* End of file fixture_model.php */
/* Location: ./system/application/models/fixture_model.php */